export default {
	namespaced: true,

	state: () => ({
		// 首页文章数据
		homeArticleData: JSON.parse(uni.getStorageSync('homeArticleData') || '[]'),
	}),

	mutations: {
		// 添加首页文章数据
		homeArticle(state, homeArticle) {
			const findResult = state.homeArticleData.find(i => i.id === homeArticle.id)
			if (!findResult) {
				state.homeArticleData.push(homeArticle)
			}
			this.commit("m_community/saveHomeArticle")
		},

		// 点赞 / 取消点赞
		updatePreferState(state, id) {
			const findResult = state.homeArticleData.find(i => i.id === id)
			if (findResult.praise.type === "") {
				state.homeArticleData[findResult.id].praise.type = "activation"
				state.homeArticleData[findResult.id].praise.num++
				if (state.homeArticleData[findResult.id].opposition.type = "activation") {
					state.homeArticleData[findResult.id].opposition.type = ""
					state.homeArticleData[findResult.id].opposition.num--
				}
			} else {
				state.homeArticleData[findResult.id].praise.type = ""
				state.homeArticleData[findResult.id].praise.num--
			}
			this.commit("m_community/saveHomeArticle")
		},

		// 踩 / 取消踩
		updateOppositionState(state, id) {
			const findResult = state.homeArticleData.find(i => i.id === id)
			if (findResult.opposition.type === "") {
				state.homeArticleData[findResult.id].opposition.type = "activation"
				state.homeArticleData[findResult.id].opposition.num++
				if (state.homeArticleData[findResult.id].praise.type = "activation") {
					state.homeArticleData[findResult.id].praise.type = ""
					state.homeArticleData[findResult.id].praise.num--
				}
			} else {
				state.homeArticleData[findResult.id].opposition.type = ""
				state.homeArticleData[findResult.id].opposition.num--
			}
			this.commit("m_community/saveHomeArticle")
		},

		// 缓存首页文章数据
		saveHomeArticle(state) {
			uni.setStorageSync('homeArticle', JSON.stringify(state.homeArticleData))
		}
	},

	getters: {}
}
